import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class ListViewTestWidget extends StatelessWidget {
  ListViewTestWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Sample ListView',
        theme: new ThemeData(primarySwatch: Colors.blue),
        home: new ListViewTestPage());
  }
}

class ListViewTestPage extends StatefulWidget {
  ListViewTestPage({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _ListViewTestState();
  }
}

class _ListViewTestState extends State<ListViewTestPage> {
  List widgets = [];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Sample App"),
      ),
      body: new ListView.builder(
        itemCount: widgets.length,
        itemBuilder: (BuildContext context, int position) {
          return getRow(position);
        },
      ),
    );
  }

  Widget getRow(int i) {
    return new Padding(
        padding: new EdgeInsets.all(10.0),
        child: new Text("Row ${widgets[i]["title"]}"));
  }

  @override
  void initState() {
    super.initState();

    loadData();
  }

  loadData() async {
    String dataURL = "https://jsonplaceholder.typicode.com/posts";
    http.Response response = await http.get(dataURL);
    setState(() {
      widgets = json.decode(response.body);
    });
  }
}
